package com.itcoon.cloud.framework.webmvc.exception.handler;

import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.i18n.AbstractLocaleContextResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.TimeZone;

/**
 * 替换 spring 默认的AcceptHeaderLocaleResolver,
 * 其时区可以通过 RequestContextUtils.getTimeZone(request) 获取
 */
@Component("localeResolver")
public class GlobalLocaleContextResolver extends AbstractLocaleContextResolver {
    @Override
    public LocaleContext resolveLocaleContext(HttpServletRequest request) {
        String timeZone = request.getHeader("time-zone");
        return new SimpleTimeZoneAwareLocaleContext(request.getLocale(), timeZone == null ? TimeZone.getDefault() : TimeZone.getTimeZone(timeZone));
    }

    @Override
    public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>" + localeContext);
    }
}
